const db = uniCloud.database();
const dbCmd = db.command
const classifyDB = db.collection('files-classify');
const filesDB = db.collection('admin-files');
import request from '../../request.js';
import Idb from '../../idb/files-list.js'
export default {
	data() {
		return {
			showMore: true,
			filesList: [],
			page: 1,
			count: 0,
			useUniCloud: false, //使用云端数据
			initCloud: false, //是否从云端初始化
		}
	},
	methods: {
		_initFiles() {
			if (this.useUniCloud) {
				this._initFilesByUniCloud();
			} else {
				this._initFilesByIndexedb();
			}
		},
		//使用本地图库
		async _initFilesByIndexedb() {
			uni.showLoading({
				mask: true
			})
			const uid = this.userInfo._id;
			const parent_id = this.checkTreeNode.parent_id || null;
			const classify = this.classify;
			if (!this.initCloud) {
				await this._initCloud();
			}

			const table = Idb.files.where({
				uid
			});
			let filter;
			if (!classify || !parent_id) {
				filter = function(item) {
					return !item.classify || item.classify === classify;
				}
			} else {
				filter = function(item) {
					return item.classify === classify;
				}
			}
			table.and(filter)
			this.count = await table.count();
			this.filesList = await table.toArray();
			uni.hideLoading();
		},
		//从云端下载图片列表
		async _initCloud(refresh = true) {
			const uid = this.userInfo._id;
			const {
				result
			} = await filesDB.where({
				uid
			}).count().catch();
			const total = result.total;
			this.initCloud = true;
			if (!refresh) {
				await Idb.files.where("uid").equals(uid).delete();
				await this._getFilesListBycloid(total);
			} else {
				const count = await Idb.files.count().catch();
				if (total !== 0 && total !== count) {
					await Idb.files.where("uid").equals(uid).delete();
					await this._getFilesListBycloid(total);
				}
			}

		},
		// 从云端下载图片
		async _getFilesListBycloid(total, offset = 0) {
			const uid = this.userInfo._id;
			let result, res;
			if (offset) {
				res = await filesDB.where({
					uid
				}).skip(offset).get();
			} else {
				res = await filesDB.where({
					uid
				}).get();
			}
			result = res.result
			await Idb.files.bulkAdd(result.data);
			offset = offset + 100;
			if (offset < total) {
				await this._getFilesListBycloid(total, offset);
			}
		},
		//图片下载初始化 
		async _initFilesByUniCloud() {
			uni.showLoading({
				mask: true
			})
			const uid = this.userInfo._id,
				classify = this.classify;
			const where = {
				uid,
				classify
			};
			const parent_id = this.checkTreeNode.parent_id || null;
			if (!classify || !parent_id) {
				where.classify = dbCmd.exists(false);
			}
			const {
				result
			} = await filesDB.where(where).get({
				getCount: true
			})
			this.count = result.count;
			this.filesList = result.data || [];
			uni.hideLoading();
		},
		//返回文件类型
		itemTypeOf(type) {
			type = type || ''
			return type.substr(0, type.indexOf("/"));
		},
		//移动文件
		moveFiles() {
			this.$refs.popup.open()
		},
		// 选择切换的目录(移动文件时)
		async clickPopupTree(node) {
			this.$refs.popup.close()
			const classify = node._id;
			const ids = this.checkboxValue;
			uni.showLoading({
				mask: true
			})
			await filesDB.where({
				_id: dbCmd.in(ids)
			}).update({
				classify
			})
			if (!this.useUniCloud) {
				Idb.files.where('_id').anyOfIgnoreCase(ids).modify({
					classify
				})
			}
			uni.hideLoading();
			this._initFiles();
		},
		//  上传文件完成时
		async uploadComplete(e) {
			const uid = this.userInfo._id;
			if (!this.useUniCloud) {
				let data = e.map(_ => {
					_.uid = uid;
					return _
				});
				data=JSON.parse(JSON.stringify(data));//此处不知道为什么错误 非得这样一次。
				await Idb.files.bulkAdd(data)
			}
			this._initFiles();
		},
		//预览图片
		previewImage(fileID) {},
		//预览文件
		previewVideo(item) {

		},
		//删除文件
		async deleteAllFiles() {
			const ids = this.checkboxValue;
			if (ids.length === 0) {
				return;
			}
			uni.showLoading({
				mask: true
			})
			await request('files/remove', {
				ids
			})
			if (!this.useUniCloud) {
				await Idb.files.where('_id').anyOfIgnoreCase(ids).delete()
			}
			this._initFiles();
			uni.hideLoading()
		}
	}
}
